package com.hd.crdao.activity;

import android.content.Intent;
import android.graphics.Color;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.bigkoo.pickerview.OptionsPickerView;
import com.google.gson.Gson;
import com.hd.crdao.R;
import com.hd.crdao.adapter.LoanCarBillRVAdapter;
import com.hd.crdao.base.BaseSecondActivity;
import com.hd.crdao.bean.LoanCarBillsBean;
import com.hd.crdao.config.Config;
import com.hd.crdao.config.Constants;
import com.hd.crdao.utils.MyToast;
import com.hd.crdao.utils.SPUtils;
import com.hd.crdao.utils.StringUtils;
import com.hd.crdao.utils.UIUtils;
import com.hd.crdao.view.ExpandHeadView;
import com.hd.crdao.view.RecyclerViewDivider;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadmoreListener;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import okhttp3.Call;

/**
 * @author Programming
 * @date 2017/10/18 0018
 * 账单页面
 */
public class LoanCarBillsActivity extends BaseSecondActivity implements View.OnClickListener, OnRefreshLoadmoreListener {
    @BindView(R.id.loanCarbill_head)
    ExpandHeadView mLoanCarBillHead;
    @BindView(R.id.currentDate)
    TextView mCurrentDate;
    @BindView(R.id.showCalendar)
    TextView mShowCalendar;
    @BindView(R.id.creditAmount)
    TextView mCreditAmount;
    @BindView(R.id.interestAmount)
    TextView mInterestAmount;
    @BindView(R.id.total)
    TextView mTotal;
    @BindView(R.id.loanCarBillRecyclerview)
    RecyclerView mLoanCarBillRecyclerview;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout mSmartRefreshLayout;
    public OptionsPickerView mOptionsPickerView;
    public List<String> mSearchDateList;


    @Override
    protected int setContentView() {
        return R.layout.activity_loancarbills;
    }

    @Override
    protected ExpandHeadView setExpandHeadView() {
        return mLoanCarBillHead;
    }

    @Override
    protected void initView() {
        mShowCalendar.setOnClickListener(this);
        initData();
    }

    private void initData() {
        mLoanCarBillRecyclerview.setLayoutManager(new LinearLayoutManager(this));
        mLoanCarBillRecyclerview.addItemDecoration(new RecyclerViewDivider(this, LinearLayoutManager.HORIZONTAL, 1, Color.GRAY));
        mSmartRefreshLayout.setOnRefreshLoadmoreListener(this);
        onRefresh(mSmartRefreshLayout);


    }

    private void loadData(final String searchDate) {
        HashMap<String, String> map = new HashMap<>();
        map.put("carBusinessId", SPUtils.getString(Config.businessId, ""));
        if (!TextUtils.isEmpty(searchDate)) {
            map.put("searchDate", searchDate);
        }
        OkHttpUtils.post().url(Constants.FINDBILLLIST_URL).params(map).build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                MyToast.show("网络开了个小差");
            }

            @Override
            public void onResponse(String response, int id) {
                LoanCarBillsBean loanCarBillsBean = new Gson().fromJson(response, LoanCarBillsBean.class);
                if (loanCarBillsBean.getSuccess() == 0) {
                    mSearchDateList = loanCarBillsBean.getSearchDateList();
                    if (mSearchDateList != null && mSearchDateList.size() != 0) {
                        mCurrentDate.setText(mSearchDateList.get(0));
                    }
                    final List<LoanCarBillsBean.ListVehicleTblBean> listVehicleTbl = loanCarBillsBean.getListVehicleTbl();
                    mCreditAmount.setText(StringUtils.doubleToString(loanCarBillsBean.getCreditAmount()));
                    mInterestAmount.setText(StringUtils.doubleToString(loanCarBillsBean.getInterestAmount()));
                    mTotal.setText("共" + String.valueOf(loanCarBillsBean.getTotal()) + "辆");
                    LoanCarBillRVAdapter mLoanCarBillRVAdapter = new LoanCarBillRVAdapter(listVehicleTbl);
                    mLoanCarBillRecyclerview.setAdapter(mLoanCarBillRVAdapter);
                    mLoanCarBillRVAdapter.setOnItemClickListener(new LoanCarBillRVAdapter.OnItemClickListener() {
                        @Override
                        public void onItemClick(View v, int position) {
                            Intent intent = new Intent(UIUtils.getContext(), LoanCarBillDetailActivity.class);
                            intent.putExtra("vehCreditId", listVehicleTbl.get(position).getVehCredit().getId());
                            intent.putExtra("searchDate", mCurrentDate.getText().toString());
                            startActivity(intent);
                        }
                    });
                } else {
                    MyToast.show(loanCarBillsBean.getMessage());
                }
            }
        });


    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.showCalendar:
                selectCalendar(mSearchDateList);
                break;
        }
    }

    private void selectCalendar(final List<String> searchDateList) {
        if (searchDateList != null) {
            mOptionsPickerView = new OptionsPickerView.Builder(this, new OptionsPickerView.OnOptionsSelectListener() {

                @Override
                public void onOptionsSelect(int options1, int options2, int options3, View v) {
                    mCurrentDate.setText(searchDateList.get(options1));
                    mOptionsPickerView.dismiss();
                    onRefresh(mSmartRefreshLayout);
                }
            }).build();
            mOptionsPickerView.setPicker(searchDateList);
            mOptionsPickerView.setSelectOptions(0);
            mOptionsPickerView.show();

        }


    }

    @Override
    public void onLoadmore(RefreshLayout refreshlayout) {
        refreshlayout.finishLoadmore();
    }

    @Override
    public void onRefresh(RefreshLayout refreshlayout) {
        loadData(mCurrentDate.getText().toString().trim());
        refreshlayout.finishRefresh();
    }
}
